#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: receiver.py
# @time: 2021/3/19 16:06
# @desc:
import json

import pika
import requests

from views.configs import CONSUME_QUEUE

MQ_URL = "161.189.87.239"
MQ_PORT = 5672

MQ_ACCOUNT = "admin"
MQ_PASSWORD = "rabbitmq666"

BACKEND_URL = "10.8.1.11"
BACKEND_PORT = 18000

credentials = pika.PlainCredentials(MQ_ACCOUNT, MQ_PASSWORD)
parameters = pika.ConnectionParameters(MQ_URL, MQ_PORT, "/", credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

channel.queue_declare(queue=CONSUME_QUEUE, durable=True)


def callback(channel, method, properties, body):
    requests.request(
        method="put",
        # url=f"http://localhost:{BACKEND_PORT}/message_queue/v1/update_data",
        url=f"http://{BACKEND_URL}:{BACKEND_PORT}/message_queue/v1/update_data",
        # data=json.dumps(content),
        data=str(body, encoding="utf-8").encode('utf-8'),
    )


if __name__ == '__main__':
    channel.basic_consume(
        on_message_callback=callback,
        queue=CONSUME_QUEUE,
        auto_ack=True,
    )

    print('[*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
